package com.truyen.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.ehcache.annotations.Cacheable;
import com.truyen.persistence.dao.ChapterTmpTruyenTrinhThamDao;
import com.truyen.persistence.entity.ChapterTmpTruyenTrinhTham;
import com.truyen.persistence.entity.TruyenTrinhTham;
import com.truyen.service.ChapterTmpTruyenTrinhThamService;
import com.truyen.utils.PagingInfo;


@Service
public class ChapterTmpTruyenTrinhThamServiceImpl implements ChapterTmpTruyenTrinhThamService {
	
	@Autowired
	private ChapterTmpTruyenTrinhThamDao chapterTmpTruyenTrinhThamDao;
	
	@Override
	public void save(ChapterTmpTruyenTrinhTham entity) {
		chapterTmpTruyenTrinhThamDao.save(entity);
	}

	@Override
	@Cacheable(cacheName="chapterTmpTruyenTrinhThamByIdCacheDataController")
	public ChapterTmpTruyenTrinhTham findById(Long id) {
		return chapterTmpTruyenTrinhThamDao.findById(id);
	}

	@Override
	public List<ChapterTmpTruyenTrinhTham> getAll() {
		return chapterTmpTruyenTrinhThamDao.findAll();
	}

	@Override
	public ChapterTmpTruyenTrinhTham findByProperty(String column, Object value) {
		List<ChapterTmpTruyenTrinhTham> lst = chapterTmpTruyenTrinhThamDao.findByQuery(column, value);
		return (lst.size() > 0)?lst.get(0):null;
	}

	@Override
	public List<ChapterTmpTruyenTrinhTham> getLstChapterByTruyen(TruyenTrinhTham entity, String kwSearch, String sort, String sortBy, PagingInfo pInfo) {
		return chapterTmpTruyenTrinhThamDao.getLstChapterByTruyen(entity,kwSearch,sort,sortBy,pInfo);
	}

	@Override
	public ChapterTmpTruyenTrinhTham findAliasvTruyen(TruyenTrinhTham entity, String alias) {
		return chapterTmpTruyenTrinhThamDao.findAliasvTruyen(entity, alias);
	}

	@Override
	@Cacheable(cacheName="chapterTmpTruyenTrinhThamAllCacheDataController")
	public List<ChapterTmpTruyenTrinhTham> getLstAllChapterTmpTruyenTrinhTham(String kwSearch, String sort, String sortBy, PagingInfo pInfo) {
		return chapterTmpTruyenTrinhThamDao.getLstAllChapterTmpTruyenTrinhTham(kwSearch, sort, sortBy, pInfo);
	}

	@Override
	public void updateChapterNumber(Long id, float chapterNumberChange) {
		try{
			ChapterTmpTruyenTrinhTham chapter = findById(id);
			chapter.setChapterNumber(chapterNumberChange);
			chapterTmpTruyenTrinhThamDao.merge(chapter);
		}catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	@Override
	@Cacheable(cacheName="chapterTmpTruyenTrinhThamCacheTruyenController")
	public List<ChapterTmpTruyenTrinhTham> getByTruyen(TruyenTrinhTham entity) {
		return chapterTmpTruyenTrinhThamDao.getByTruyen(entity);
	}

	@Override
	public ChapterTmpTruyenTrinhTham getChapterByTruyenChapnumber(TruyenTrinhTham entity, float chapterNumber) {
		return chapterTmpTruyenTrinhThamDao.getChapterByTruyenChapnumber(entity,chapterNumber);
	}

	@Override
	@Transactional
	public void delete(long id) {
		try{
			ChapterTmpTruyenTrinhTham chapter = findById(id);
			if(chapter != null){
				chapterTmpTruyenTrinhThamDao.delete(chapter);
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Cacheable(cacheName="chapterTmpTruyenTrinhThamMoiCapNhatCacheTruyenController")
	public List<ChapterTmpTruyenTrinhTham> getListNewChapter(int maxLimit) {
		return chapterTmpTruyenTrinhThamDao.getListNewChapter(maxLimit);
	}
	
	@Override
	public void update(Long id, Float chapterNumber) {
		try{
			ChapterTmpTruyenTrinhTham chapter = findById(id);
			if(chapter != null){
				chapter.setChapterNumber(chapterNumber);
				chapterTmpTruyenTrinhThamDao.merge(chapter);
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public List<ChapterTmpTruyenTrinhTham> getLstByOrderBy(String columnOrderBy, PagingInfo pInfo) {
		return chapterTmpTruyenTrinhThamDao.getLstByOrderBy(columnOrderBy, pInfo);
	}

	@Override
	public List<ChapterTmpTruyenTrinhTham> getByTruyenPaging(TruyenTrinhTham entity, PagingInfo pInfoChapter) {
		return chapterTmpTruyenTrinhThamDao.getByTruyenPaging(entity,pInfoChapter);
	}

	@Override
	public ChapterTmpTruyenTrinhTham findByTruyenChapterNumber(TruyenTrinhTham entity, float chapterNumber) {
		return chapterTmpTruyenTrinhThamDao.findByTruyenChapterNumber(entity,chapterNumber);
	}

	@Override
	public List<Object> getByIdTruyen(Long idTruyen) {
		return chapterTmpTruyenTrinhThamDao.getByIdTruyen(idTruyen);
	}

}
